package com.cs.study.homework._0301;

import java.util.Arrays;
import java.util.Random;

public class Demo3 {
    public static void main(String[] args) {
        String[] s = new String[8];
        for (int i = 0; i < 8; i++) {
            s[i] = getRandom(5);
        }
        System.out.println("排序前");
        System.out.println(Arrays.toString(s));
        System.out.println("排序后");
        System.out.println(Arrays.toString(bubbleSort(s)));


    }

    private static String getRandom(int length) {
        String s = "";
        for (int i = 0; i <= 9; i++) {
            s += ((char) i);
        }
        for (int i = 'a'; i <= 'z'; i++) {
            s += ((char) i);
        }
        for (int i = 'A'; i <= 'Z'; i++) {
            s += ((char) i);
        }

        char[] chars = new char[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(s.length());
            chars[i] = s.charAt(index);
        }
        String s1 = new String(chars);
        return s1;
    }

    public static String[] bubbleSort(String[] arr) {
        for (int i = 1; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                char c1 = arr[i].charAt(0);
                char c2 = arr[i + 1].charAt(0);
                if (c1 > c2) {
                    String temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                }
            }

        }
        return arr;
    }
}
